package org.jing.core.lang.data;

import org.jing.core.comp.JComponent;
import org.jing.core.lang.JingException;
import org.jing.core.lang.itf.JSerializableFactory;
import org.jing.core.util.DateUtil;
import org.jing.core.util.GenericUtil;
import org.jing.core.util.StringUtil;

import java.util.Date;

/**
 * Description: <br>
 *
 * @author bksmate <br>
 * @since 2025-01-09 <br>
 */
@JComponent
public class DateSerializableFactory implements JSerializableFactory<Date> {
    @Override public Object toCarrier(Date input, String... args) {
        return null == input ? null : DateUtil.getDateString(input, GenericUtil.get(args,0, DateUtil.JAVA_DATE_TIME));
    }

    @Override public Date fromCarrier(Object input, String... args) {
        if (null == input) {
            return null;
        }
        else if (input instanceof Date) {
            return (Date) input;
        }
        else if (input instanceof String) {
            if (StringUtil.isEmpty((String) input)) {
                return null;
            }
            return DateUtil.getDate((String) input, GenericUtil.get(args,0, DateUtil.JAVA_DATE_TIME));
        }
        throw new JingException("invalid type of carrier value: " + input.getClass());
    }
}